package cn.phub.org.controller;


import cn.phub.basic.util.AjaxResult;
import cn.phub.basic.util.PageList;
import cn.phub.org.domain.DictionaryType;
import cn.phub.org.query.DictionaryQuery;
import cn.phub.org.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private ITypeService typeService;

    /*
     * 查询所有数据
     * */
    @GetMapping
    public List<DictionaryType> queryAll(){
        return typeService.queryAll();
    }
    /*
     * 高级查询+分页
     * */
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryQuery query) {
        return typeService.queryPage(query);
    }

    /*
    * 添加修改
    * */
    @PutMapping
    //@RequestBody从请求体中拿数据
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId() == null){
                typeService.add(dictionaryType);
            }else{
                typeService.update(dictionaryType);
            }
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /*
    * 删除
    * */
    @DeleteMapping("/{id}")
    //@PathVariable 用来获取请求地址中占位符的值
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            typeService.remove(id);
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("访问人数过多，系统崩溃了");
        }
    }
    /*
    * 批量删除
    * */
    @PatchMapping
    //@PathVariable 用来获取请求地址中占位符的值
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            typeService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("访问人数过多，系统崩溃了");
        }
    }
    /*
    * 查询一条数据
    * */
    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id){
        return typeService.queryById(id);
    }

}
